/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.manager.create;

import br.com.deployment.daoImpl.CategoryDAOImpl;
import br.com.deployment.entities.Category;
import br.com.deployment.entities.Issues;
import br.com.deployment.entities.Questionnaire;
import br.com.deployment.utils.Msg;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author ely
 */
@ManagedBean
@SessionScoped
public class ManagerCreateCategory {

    @EJB
    CategoryDAOImpl categoryService;
    // Objeto
    private Category category;
    private String titleIssues;
    private Issues issues;
    // Lista de Objetos
    private List<Category> categorys;
    private List<Issues> issueses;

    public ManagerCreateCategory() {
    }

    @PostConstruct
    public void init() {
        category = new Category();
        categorys = new ArrayList<Category>();
        issues = new Issues();
        issueses = new ArrayList<Issues>();

    }

    // Método para Salvar Objeto Categoria
    public void save() {
        if (category.getName().equals("")) {
            Msg.messagemWarn("O campo categoria não pode ser vazio");
        } else {
            categoryService.save(category);
             Msg.messagemInfo("Categoria Salvo com Sucesso");
            category = new Category();
        }
    }

    // Auto Complemento Objeto Categoria
    public List<Category> completeCategory(String query) {
        List<Category> bd = new ArrayList<Category>();
        Category cat = new Category();
        cat.setName(query);
        try {
            bd = categoryService.findCategoryComplete(cat);
        } catch (Exception ex) {
            Logger.getLogger(ManagerCreateCategory.class.getName()).log(Level.SEVERE, null, ex);
        }
        return bd;
    }

//     public void addIssues() {
//        issues.setId(1L);
//        issues.setTitle(titleIssues);
//        issueses.add(issues);
//        categoryService.update(this.category);
//
//    }
//    // Retorna Issues e Joga para o TypeIssues o seu tipo;
//    public Issues returnTypeIssues(Issues issues, TypeIssues type) {
//        issues.setTypeIssues(type);
//        return issues;
//    }
    // Redireciona a Página 
    public void redirect(String string) {
        try {
            FacesContext context = FacesContext.getCurrentInstance();

            context.getExternalContext().redirect(string);
        } catch (IOException ex) {
            Logger.getLogger(ManagerCreateCategory.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    // Redireciona a página e pega o Objeto Selecionado
    public void redirect(String string, Category c) {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            this.category = c;
            context.getExternalContext().redirect(string);
        } catch (IOException ex) {
            Logger.getLogger(ManagerCreateQuestionnaire.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void xQuestionnaire(Questionnaire q) {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            System.out.println(q);
            categorys = categoryService.findCategoryQuestionnaire(q);
            context.getExternalContext().redirect("editIssues.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(ManagerCreateCategory.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void xCategory(Category c) {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            this.category = c;

            context.getExternalContext().redirect("createThemes.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(ManagerCreateCategory.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void redirect(String string, Questionnaire q) {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            System.out.println(q);
            context.getExternalContext().redirect(string);
        } catch (IOException ex) {
            Logger.getLogger(ManagerCreateCategory.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public List<Category> getCategorys() {
        return categorys;
    }

    public void setCategorys(List<Category> categorys) {
        this.categorys = categorys;
    }

    public Issues getIssues() {
        return issues;
    }

    public void setIssues(Issues issues) {
        this.issues = issues;
    }

    public List<Issues> getIssueses() {
        return issueses;
    }

    public void setIssueses(List<Issues> issueses) {
        this.issueses = issueses;
    }

    public String getTitleIssues() {
        return titleIssues;
    }

    public void setTitleIssues(String titleIssues) {
        this.titleIssues = titleIssues;
    }
}
